package com.fruit.web.vo.goods.importfruit;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;

import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import com.fruit.utils.GoodsUtil;
import com.fruit.utils.SpringFacesUtil;
import com.fruit.web.constant.EjdConstants;
import com.fruit.web.vo.goods.ListGoodsPageBean;
@ManagedBean(name="listImportFruitPage")
@SessionScoped
public class ListImportFruitPageBean extends ListGoodsPageBean implements java.io.Serializable {
	private static final long serialVersionUID = -8106629465344224715L;
	
	private List<SelectItem> conditionItems;
	
	
	public List<SelectItem> getConditionItems() {
		return conditionItems;
	}


	public void setConditionItems(List<SelectItem> conditionItems) {
		this.conditionItems = conditionItems;
	}


	public ListImportFruitPageBean(){
		this.setPrimaryCategoryCode(EjdConstants.PRIMARY_CATEGORY_CODE_IMPORT_FRUIT);
		this.initBrandCodes();
		this.initSecondCategory();
		String sessionId = SpringFacesUtil.getCurrentSessionId();
		this.setSessionId(sessionId);
		
		
		// for show data
		this.setPromotionGoods(GoodsUtil.getGoods(EjdConstants.PRIMARY_CATEGORY_CODE_IMPORT_FRUIT, "%" + EjdConstants.SHOW_ON_PROMOTION + "%"));
		this.setHotGoods(GoodsUtil.getGoods(EjdConstants.PRIMARY_CATEGORY_CODE_IMPORT_FRUIT, "%" + EjdConstants.SHOW_ON_HOT + "%"));
		this.setRecommendGoods(GoodsUtil.getGoods(EjdConstants.PRIMARY_CATEGORY_CODE_IMPORT_FRUIT, "%" +EjdConstants.SHOW_ON_RECOMMEND + "%"));
		//end
		
		this.setDefaultHql("from Goods where 1=1 ");
		this.setHql(this.getDefaultHql());
		this.setPageNo(1);
		this.setPriceBetween("N");
		this.setPriceRange(null);
		this.setSortStyle("NO");
		this.initConditionItems();
		this.generateHQL(this.getDefaultHql());
		this.getPageDatas();
	}
	
	protected void initConditionItems(){
		List<SelectItem> result = constructConditionItems();
		this.setConditionItems(result);
	}
	public List<SelectItem> constructConditionItems() {
		List<SelectItem> result = new ArrayList<SelectItem>();
		if (null != this.getBrandCodes() && null != this.getBrandCodes()[0] && !("".equals(this.getBrandCodes()[0]))) {
			SelectItem[] tempBrandCodeItems = this.getBrandCodeItems();
			if (null != tempBrandCodeItems && tempBrandCodeItems.length > 0) {
				boolean findBrandFlag = false;
				String target = this.getBrandCodes()[0];
				for (SelectItem item:tempBrandCodeItems) {
					if (findBrandFlag) {
						break;
					}
					if (null != item && !("".equals(item.getValue()))) {
						if (target.equals(item.getValue())) {
							result.add(item);
							findBrandFlag = true;
						}
					}
				}
			}
		}
		if (null != this.getSecondCategories() && null != this.getSecondCategories()[0] && !("".equals(this.getSecondCategories()[0]))) {
			SelectItem[] tempSecondCategoryItems = this.getSecondCategoryItems();
			if (null != tempSecondCategoryItems && tempSecondCategoryItems.length > 0) {
				boolean findSecondCategoryFlag = false;
				String target = this.getSecondCategories()[0];
				for (SelectItem item:tempSecondCategoryItems) {
					if (findSecondCategoryFlag) {
						break;
					}
					if (null != item && !("".equals(item.getValue()))) {
						if (target.equals(item.getValue())) {
						result.add(item);
						findSecondCategoryFlag = true;
						}
					}
				}
			}
		}
		SelectItem[]  tempPriceBetweenItems = this.getPriceBetweenItems();
		if (null != tempPriceBetweenItems && tempPriceBetweenItems.length > 1) {
			boolean findPriceBetweenFlag = false;
			for(SelectItem item:tempPriceBetweenItems) {
				if (findPriceBetweenFlag) {
					break;
				}
				if (null != item.getValue() && !("".equals(item.getValue()))) {
					if (!("N".equals(this.getPriceBetween())) && (this.getPriceBetween().equals(item.getValue()))) {
						result.add(item);
						findPriceBetweenFlag = true;
					}
				}
			}
		}
		return result;
	}
}
